package cn.kgc.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author: mosin
 * @version: v1.0  2022/3/22
 */
@WebServlet("/login")
public class LoginServlet  extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         //请求的编码格式已在过滤器中统一处理
         // req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        System.out.println("username = " + username);
        HttpSession session = req.getSession();
        if("admin".equals(username)&&"123456".equals(password)){ // 登录成功

            //将用户名保存在session中 用于用户登录的验证
            session.setAttribute("username",username);
            //将页面跳转至 主页 user.jsp
            resp.sendRedirect("/user.jsp");

        }else{

            //req.setAttribute("msg","用户名或密码错误");
            session.setAttribute("msg","用户名或密码错误");
            req.getRequestDispatcher("/login.jsp").forward(req,resp);

        }

    }
}
